<?php
namespace app\admin\controller;
use think\Db;
class Advertise extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(session ('area')){
                $map['ad_qu_id'] = ['in',session('area')];
            }
            if(isset($area)&&$area != '0'&&$area != ''){
                $map['ad_qu_id'] = $area;
            }else if(isset($area)&&$area == '0'&&$area != ''){
                $map['ad_qu_id'] = null;
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('ad d')
                ->join('admin a','a.id = d.ad_user_id','left')
                ->where($map)
                ->count();
            $lists =  Db::name('ad d')
                ->join('admin a','a.id = d.ad_user_id','left')
                ->where($map)
                ->field('d.*,a.username')
                ->page($Nowpage,$limits)
                ->order('ad_sort desc')
                ->select ();
            if(!empty($lists)){
                foreach($lists as $key=>$vo){
                    switch ($vo['ad_cate']){
                        case 2:
                            $lists[$key]['ad_link'] = Db::name('shop')
                                ->where('shop_id',$vo['ad_link'])
                                ->value ('shop_name');
                            break;
                        case 3:
                            $lists[$key]['ad_link'] = Db::name('product')
                                ->where('p_id',$vo['ad_link'])
                                ->value ('p_name');
                            break;
                        case 4:
                            $lists[$key]['ad_link'] = Db::name('d_treasure d')
                                ->join('product p','d.d_product_id = p.p_id','left')
                                ->where('d_id',$vo['ad_link'])
                                ->value ('p_name');
                            break;
                        case 5:
                            $lists[$key]['ad_link'] = Db::name('posts')
                                ->where('post_id',$vo['ad_link'])
                                ->value ('post_content');
                            break;
                    }
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $data = Db::name('admin a')
            ->join('area e','a.area = e.district_id')
            ->where(['id'=>['neq',1],'level'=>3])
            ->field('district_id,district')
            ->select ();
        return $this->fetch ('ad/index',['data'=>$data]);
    }


    public function adAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                'ad_img'=>$img,
                'ad_cate'=>$cate,
                'ad_link'=>$link,
                'ad_left_title'=>$left_title,
                'ad_right_title'=>$right_title."人推选",
                'ad_port'=>$port,
                'ad_type'=>session('uid') == 1? 1 : 2,
                'ad_qu_id'=>session('myarea'),
                'ad_user_id'=>session('uid'),
                'ad_add_time'=>date('Y-m-d H:i:s'),
                'ad_edit_time'=>date('Y-m-d H:i:s')
            ];
            if($port == 1){
                $param['ad_postion'] = $place;
            }else{
                $param['ad_postion'] = 3;
            }
            $res = Db::name('ad')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('ad/ad_add');
    }


    public function adEdit(){
        extract (input());
        if(request()->isPost ()){
            $param = [
                'ad_img'=>$img,
                'ad_link'=>$link,
                'ad_cate'=>$cate,
                'ad_left_title'=>$left_title,
                'ad_right_title'=>$right_title."人推选",
                'ad_port'=>$port,
                'ad_edit_time'=>date('Y-m-d H:i:s')
            ];
            if($port == 1){
                $param['ad_postion'] = $place;
            }else{
                $param['ad_postion'] = 3;
            }
            $res = Db::name('ad')->where('ad_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }else{
            $data = Db::name('ad')->where(['ad_id'=>$id])->find();
            if(!empty($data)){
                switch ($data['ad_cate']){
                    case 2:
                        $data['link'] = Db::name('shop')
                            ->where('shop_status',1)
                            ->field('shop_id as id,shop_name as name')
                            ->select ();
                        break;
                    case 3:
                        $data['link'] = Db::name('product')
                            ->where('p_status',4)
                            ->field('p_id as id,p_name as name')
                            ->select ();
                        break;
                    case 4:
                        $data['link'] = Db::name('d_treasure d')
                            ->join('product p','d.d_product_id = p.p_id','left')
                            ->where('d_status',2)
                            ->field('d_id as id,p_name as name')
                            ->select ();
                        break;
                    case 5:
                        $data['link'] = Db::name('posts')
                            ->where('post_state',1)
                            ->field('post_id as id,post_content as name')
                            ->select ();
                        break;
                }
            }
//            dump($data);die;
            return $this->fetch('ad/ad_edit',['data'=>$data]);
        }

    }

    public function adDel(){
        extract (input());
        $res = Db::name('ad')->where('ad_id',$id)->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    public function adCate(){
        extract (input());
        switch ($cate){
            case 2:
                $data = Db::name('shop')
                    ->where('shop_status',1)
                    ->field('shop_id as id,shop_name as name')
                    ->select ();
                break;
            case 3:
                $data = Db::name('product')
                    ->where('p_status',4)
                    ->field('p_id as id,p_name as name')
                    ->select ();
                break;
            case 4:
                $data = Db::name('d_treasure d')
                    ->join('product p','d.d_product_id = p.p_id','left')
                    ->where('d_status',2)
                    ->field('d_id as id,p_name as name')
                    ->select ();
                break;
            case 5:
                $data = Db::name('posts')
                    ->where('post_state',1)
                    ->field('post_id as id,post_content as name')
                    ->select ();
                break;
        }
        return json(['code'=>200,'msg'=>$data]);
    }

    public function addSort(){
        extract (input());
        $res = Db::name('ad')
            ->where('ad_id',$id)
            ->update(['ad_sort'=>$num,'ad_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'编辑成功']);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }

}